﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class TamTru : Form
    {
        private GUI.NhanKhau nhanKhau;
        static String maNhanKhau;
        model.BO.TamTruBO tamTruBO;

        public TamTru(GUI.NhanKhau f, String maNK)
        {
            this.nhanKhau = f;
            maNhanKhau = maNK;
            InitializeComponent();
        }

        private void TamTru_Load(object sender, EventArgs e)
        {
            tamTruBO = new model.BO.TamTruBO(this, nhanKhau);

            this.tbMaNhanKhau.Text = maNhanKhau;

            tamTruBO.loadDefault(maNhanKhau);
            tamTruBO.loadTable(maNhanKhau);
            
            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlTamTru.Location = new System.Drawing.Point(2, 2);
            this.gridControlTamTru.Size = new System.Drawing.Size(988, 619);
        }

        private void toolStripMenuItemThemMoi_Click(object sender, EventArgs e)
        {
            panelTop.Visible = true;
            this.gridControlTamTru.Location = new System.Drawing.Point(2, 244);
            this.gridControlTamTru.Size = new System.Drawing.Size(988, 377);

            tamTruBO.genarateMaTamTru();

            cboTinhTrang.SelectedIndex = 0;
        }

        private void gridViewTamTru_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripTamTru.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripTamTru.Show(view.GridControl, e.Point);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            tamTruBO.btnLuu();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTamTru.SelectedRowsCount > 0)
            {
                panelTop.Visible = true;
                this.gridControlTamTru.Location = new System.Drawing.Point(2, 244);
                this.gridControlTamTru.Size = new System.Drawing.Size(988, 377);

                tbMaTamTru.Text = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "MaTamTru").ToString();
                cboTinhTrang.SelectedItem = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "TinhTrang").ToString();
                dateTimeNgayTamTru.Text = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "NgayTamTru").ToString();
                tbNoiDangKiTamTru.Text = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "NoiDangKiTamTru").ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            panelTop.Visible = false;
            this.gridControlTamTru.Location = new System.Drawing.Point(2, 2);
            this.gridControlTamTru.Size = new System.Drawing.Size(988, 619);
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTamTru.SelectedRowsCount > 0)
            {
                String maTamTru = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "MaTamTru").ToString();
                String maNhanKhau = gridViewTamTru.GetRowCellValue(gridViewTamTru.FocusedRowHandle, "MaNhanKhau").ToString();

                DialogResult result = MessageBox.Show("Bạn có muốn xóa Mã Tạm Trú: " + maTamTru + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    tamTruBO.deleteRow(maTamTru, maNhanKhau);  
                }
            }
        }
    }
}
